package com.ddyx.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.ddyx.entity.Employe;
import com.ddyx.entity.FaceImage;
import com.ddyx.entity.WxUser;
import com.ddyx.mapper.EmployeMapper;
import com.ddyx.mapper.FaceImageMapper;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.service.IUserService;
import com.ddyx.util.FileUpload;
import com.ddyx.util.ImageUtils;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.PropertiesUtil;
import com.ddyx.util.QiniuUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.util.Utils;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

	@Resource
	WxUserMapper userMapper;
	
	@Resource
	EmployeMapper employeMapper;

	@Resource
	FaceImageMapper faceImageMapper;

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getUserInfo(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> resultMap=new HashMap<String, Object>();
		if (null != param) {
			try {
				WxUser bean = new WxUser();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				MyBeanUtils.populate(bean, paramMap);
				if(null!=bean && StringUtils.hasText(bean.getWxUid())) {
					WxUser bea=userMapper.selectOne(bean);
					if(null!=bea) {
						String state="30";
						String phone=bea.getMobile();
						if(StringUtils.hasText(phone)) {
							state="50";
						}else {
							phone="";
						}
						//获取人脸图
						FaceImage faceImage=new FaceImage();
						faceImage.setWxUid(bean.getWxUid());
						FaceImage feceIm=faceImageMapper.selectOne(faceImage);
						String imageUrl="";
						if(null!=feceIm && StringUtils.hasText(feceIm.getImageUrl())) {
							//bea.setFaceImage(feceIm.getImage_url());
							imageUrl=feceIm.getImageUrl();
							state="100";
							if(!StringUtils.hasText(phone)) {
								state="80";
							}
						}
						resultMap.put("state", state);
						resultMap.put("faceImage", imageUrl);
						resultMap.put("mobile", phone);
						result.setData(resultMap);
					}else {
						result.setStatus(false);
						result.setMessage("没有数据");
					}
				}else {
					result.setStatus(false);
					result.setMessage("传参有误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "static-access" })
	@Transactional
	@Override
	public ResponseForm updateUser(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			try {
				WxUser bean = new WxUser();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				MyBeanUtils.populate(bean, paramMap);
				String wxUid=bean.getWxUid();
				String phone=bean.getMobile();
				String faceImage=bean.getFaceImage();
				if(null!=bean && StringUtils.hasText(wxUid)) {
					if(StringUtils.hasText(phone)) {
						Boolean bo=new Utils().JudgePhoneNumber(phone);
						if(bo) {
							userMapper.updateByPrimaryKeySelective(bean);
						}else {
							result.setStatus(false);
							result.setMessage("电话号码格式错误");
							return result;
						}
					}
					if(StringUtils.hasText(faceImage)) {
						//根据wxUid删除用户人脸识别图的数据
						Example example = new Example(FaceImage.class);
						Criteria criteria = example.createCriteria();
						criteria.andEqualTo("wxUid", wxUid);
						faceImageMapper.deleteByExample(example);
						//添加用户的人脸识别图
						FaceImage faceImageBo=new FaceImage();
						faceImageBo.setWxUid(wxUid);
						faceImageBo.setImageUrl(faceImage);
						String savePath=PropertiesUtil.getProperty("QRCode.logo");
						String imageName=new Utils().getUUID()+".jpg";
						new ImageUtils().getImage(faceImage, savePath,imageName);
						String imgMd5=new ImageUtils().getMd5ByFile(new File(savePath+imageName));
						faceImageBo.setImageMd5(imgMd5);
						faceImageBo.setCreateTime(new Date());
						faceImageBo.setCreateUser(bean.getCreateUser());
						faceImageMapper.insertSelective(faceImageBo);
					}
				}else {
					result.setStatus(false);
					result.setMessage("传参有误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}
	
	@SuppressWarnings("static-access")
	@Override
	public ResponseForm fileUpload(String wxUid, String wxSid, HttpServletRequest request,
			HttpServletResponse response) {
		//wxUid wxSid 做验证
		ResponseForm result = new ResponseForm();
		//第一步上传图片  获取到保存后的本地路径
		try {
			String localImageUrl=new FileUpload().fileUpload(request, response);
			if(StringUtils.hasText(localImageUrl)) {
				Map<String, Object> map=new HashMap<String, Object>();
				//未调用萝卜头人脸识别
				String qiniuUrl=new QiniuUtil().getQiNiuUrl(localImageUrl);
				if(StringUtils.hasText(qiniuUrl)) {
					map.put("imgurl", qiniuUrl);
					result.setData(map);
					return result;
				}else {
					result.setMessage("上传七牛出错");
				}
				
				//调用萝卜头人脸识别
				/*int in=new RobotscloudUtil().getRobotscloudResult(localImageUrl);
				if(200==in) {
					String qiniuUrl=new QiniuUtil().getQiNiuUrl(localImageUrl);
					if(StringUtils.hasText(qiniuUrl)) {
						map.put("imgurl", qiniuUrl);
						result.setData(map);
						return result;
					}else {
						result.setMessage("上传七牛出错");
					}
				}else {
					result.setMessage("请上传正面照");
				}*/
				
				
			}else {
				result.setMessage("上传图片错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		result.setStatus(false);
		return result;
	}
	
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		String aa=new Utils().getUUID()+"1234.54545.656".
				substring("1234.54545.656".lastIndexOf("."), "1234.54545.656".length());
		System.out.println(aa);
		
		String savePath=PropertiesUtil.getProperty("QRCode.logo");
		String imageName=new Utils().getUUID()+".jpg";
		new ImageUtils().getImage("http://p1az9gcpw.bkt.clouddn.com/2JF4DMNA01650_m_3.jpg", savePath,imageName);
		try {
			String imgMd5=new ImageUtils().getMd5ByFile(new File(savePath+imageName));
			System.out.println("imgMd5--"+imgMd5);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/* 
	 * @desc 弃用
	 */
	@Override
	public Map<String, Object> getEmployInfo(String mobile) {
		Map<String, Object> resultMap = new HashMap<>();
		if(org.apache.commons.lang3.StringUtils.isNotBlank(mobile)) {
			try {
				resultMap = userMapper.getEmployInfo(mobile);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("操作有误");
			}
			if(resultMap.containsKey("storeNo")&&resultMap.containsKey("wxUid")&&resultMap.containsKey("wxSid")) {
				return resultMap;
			}
		}
		return null;
	}
	/* 
	 * @DESC 弃用
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm login(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		
		try {
			String mobile= (String) paramMap.get("mobile");
			String pwd= (String) paramMap.get("password");
			Employe employe = new Employe();
			employe.setMobile(mobile);
			employe =employeMapper.selectOne(employe);
			if(employe ==null || !pwd.equals(employe.getPassword())){
				result.setStatus(false);
				result.setMessage("用户或密码错误");
				log.error("用户或密码错误");
			}else{
				result.setData(employe);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
}
